package Server;

import Data.FriendListData;
import Data.ResetPwdData;
import Email.ManageCaptcha;
import MyInterfaces.MassageType;
import Data.event;
import Search.UserInitStage;

import java.io.*;
import java.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import Email.*;

public class ResetPwdServer {

    static String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    static String user = "root";
    static String password = "Mi20030610.";
    private static Socket socket;
    String sql = "select *from userinformation where emailNumber =?;";
    String newsql = "update userinformation set userPwd =? where emailNumber=?;";

    public ResetPwdServer(Socket socket) {
        ResetPwdServer.socket = socket;
    }

    public boolean ResetPwd(event massage) throws Exception {
        ResetPwdData resetPwdData=(ResetPwdData) massage.getT();
        String email=resetPwdData.getEmail();
        String captcha=resetPwdData.getCaptcha();
        String userPwd=resetPwdData.getUserPwd();
        String md5=getMD5(userPwd);

        //获得服务器存储的验证码
        String server_captcha= ManageCaptcha.GetcaptchaByEmail(email,new Date()).getCaptcha();

        if(!server_captcha.equals(captcha)||!server_captcha.contains(email))
        {
            //返回一个重置密码错误的消息
            event returnmassage=new event();
            returnmassage.setType(MassageType.Return_Reset_Password_Fail);
            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
            writer.writeObject(returnmassage);
            writer.flush();
            return false;
        }

        //如果成功移除验证码
        ManageCaptcha.RemoveCaptchaByEmail(email);

        Connection Conn = DriverManager.getConnection(url, user, password);

        //先查询是否有这个邮箱
        PreparedStatement operation = null;
        operation = Conn.prepareStatement(sql);
        operation.setString(1, resetPwdData.getEmail());
        ResultSet resultset = operation.executeQuery();
        if (resultset.next())
        {
            System.out.println("找到了");

            //数据库重置密码
            PreparedStatement newoper = null;
            newoper = Conn.prepareStatement(newsql);

            newoper.setString(1, md5);
            newoper.setString(2, email);

            String id=resultset.getString("id");
            int k = newoper.executeUpdate();

            if(k>0)
            {
                //重置密码成功
                event returnmassage=new event();
                returnmassage.setType(MassageType.Return_Reset_Password_Success);
                UserInitStage userInitStage=new UserInitStage(id);
                FriendListData friendListData=userInitStage.userInformation();
                returnmassage.setT(friendListData);
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();

                System.out.println("接受客户端发送的消息");
                ChatServer chatServer=new ChatServer(socket,id);

                //开启一个线程用来接收消息
                new Thread(chatServer).start();
                ManageClientConnectionServerThread.addThread(id,chatServer);
                return true;
            }
            else
            {
                //重置密码错误
                event returnmassage=new event();
                returnmassage.setType(MassageType.Return_Reset_Password_Fail);
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();

                return false;
            }
        }
        else
        {
            //找不到邮箱
            event returnmassage=new event();
            returnmassage.setType(MassageType.Return_Reset_Password_Fail);
            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
            writer.writeObject(returnmassage);
            writer.flush();
            return false;
        }
    }

    public String getMD5(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        return new BigInteger(1, md.digest()).toString(16);
    }
}
